<template>
  <div class="login">
    <div class="top">
      登录
    </div>
    <div class="middle">
      <van-form ref="form">
        <van-field
          class="box1"
          name="mobile"
          placeholder="请输入手机号"
          v-model="form.mobile"
          :rules="rules.mobile"
        >
          <template #left-icon>
            <i class="iconfont">&#xe759;</i>
          </template>
        </van-field>
        <van-field
          name="code"
          class="box1"
          placeholder="请输入验证码"
          left-icon="lock"
          v-model="form.code"
          :rules="rules.code"
        >
          <template #button>
            <span class="code">发送验证码</span>
          </template>
        </van-field>
      </van-form>
    </div>
    <div class="fotter">
      <van-button class="btn" block color="#1989fa" @click="btnclick"
        >登录</van-button
      >
    </div>
  </div>
</template>

<script>
import { appV10Authorizations } from '@/api/login.js'
import { setLocalToken } from '@/utils/token.js'
export default {
  name: 'Login',
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          { require: true, message: '请输入手机号', trigger: 'onChange' },
          {
            validator: value => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                value
              )
            },
            message: '请正确输入手机号',
            trigger: 'onChange'
          }
        ],
        code: [{ require: true, message: '请输入验证码', trigger: 'onChange' }]
      }
    }
  },
  methods: {
    btnclick () {
      this.$refs.form.validate().then(async () => {
        const res = await appV10Authorizations(this.form)
        console.log(res)
        // 保存token
        setLocalToken('token', res.data.token)
        // fixme todo 1-1 设置refresh_token
        setLocalToken('refresh_token', res.data.refresh_token)
        // 设置登录状态
        this.$store.commit('setIsLogin', true)
        this.$router.push('./home/my')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.top {
  height: 64px;
  background: @color-bg;
  font-family: MicrosoftYaHei;
  font-size: 16px;
  font-weight: normal;
  font-stretch: normal;
  line-height: 64px;
  letter-spacing: 1px;
  color: #ffffff;
  text-align: center;
}
.middle {
  padding: 10px;
  .box1 {
    border-bottom: 1px solid #ccc;
    .code {
      padding: 6px;
      font-family: MicrosoftYaHei;
      font-size: 11px;
      font-weight: normal;
      font-stretch: normal;
      line-height: 18px;
      letter-spacing: 1px;
      color: #666666;
      background-color: #ededed;
      border-radius: 12px;
    }
  }
}
.fotter {
  padding: 10px;
  .btn {
    background: linear-gradient(135deg, #52e5e7 10%, #130cb7 100%) !important;
  }
}
</style>
